home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / readers / skim-0.8 / skim-0 / skim-0.8.4 / ArticleTest.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  7KB  |  276 lines

  1. /*
  2.  * NAME
  3.  *   ArticleTest.c
  4.  * DESCRIPTION
  5.  *   Test the Article ADT. Run this test by typing 'make test' in the source
  6.  *   directory of skim.
  7.  * COPYRIGHT
  8.  *   Copyright (C) 1996  Rene W.J. Pijlman
  9.  *
  10.  *   This program is free software; you can redistribute it and/or modify
  11.  *   it under the terms of the GNU General Public License as published by
  12.  *   the Free Software Foundation; either version 2 of the License, or
  13.  *   (at your option) any later version.
  14.  *
  15.  *   This program is distributed in the hope that it will be useful,
  16.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *   GNU General Public License for more details.
  19.  *
  20.  *   You should have received a copy of the GNU General Public License
  21.  *   along with this program; if not, write to the Free Software
  22.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  * VERSION
  24.  *   $Header: /home/rene/sys/CVS_MasterSourceRepository/skim/ArticleTest.c,v 1.7 1996/02/18 11:40:16 rene Exp $
  25.  *   Distributed with Skim version 0.8.4.
  26.  */
  27.  
  28. #include "Article.h"
  29. #include "StandardIO.h"
  30. #include "MemAlloc.h"
  31.  
  32. #include <assert.h>
  33. #include <string.h>
  34.  
  35. /* assert()'s must be in effect in this file. */
  36. #ifdef NDEBUG
  37. #    undef NDEBUG
  38. #endif
  39.  
  40. static void TestCreation( Article * Article1 )
  41. {
  42.     ArticleDestroy( ArticleCreate() );
  43.  
  44.     *Article1 = ArticleCreate();
  45. }
  46.  
  47. static void TestMinimalNewArticle(void)
  48. {
  49.     VarBuf Newsgroups = VBCreateString("test");
  50.     VarBuf Subject = VBCreateString("test, please ignore");
  51.     VarBuf From = VBCreateString("me");
  52.     VarBuf NewVB = NULL;
  53.     VarBuf Date = VBCreateString("today");
  54.  
  55.     Article New = ArticleConstructNew( Newsgroups, Subject, From, NULL,
  56.                                        NULL, NULL, NULL, NULL );
  57.  
  58.     ArticleSetHeaderLine( New, HL_DATE, Date );
  59.  
  60.     NewVB = ArticleGetVB( New );
  61.  
  62.     if ( strcmp( VBAsString(NewVB),
  63.      "From: me\n"
  64.      "Date: today\n"
  65.      "Lines: 2\n"
  66.      "Newsgroups: test\n"
  67.      "Subject: test, please ignore\n"
  68.      "\n"
  69.      DEFAULT_BODY_FOR_NEW_ARTICLE ))
  70.     {
  71.     SIOPrintf( StandardError, 
  72.                        "Minimal new article is not correct:\n%V",
  73.                NewVB );
  74.     exit( EXIT_FAILURE );
  75.     }
  76.  
  77.     VBDestroy( Newsgroups );
  78.     VBDestroy( Subject );
  79.     VBDestroy( From );
  80.     VBDestroy( NewVB );
  81.     VBDestroy( Date );
  82.     ArticleDestroy( New );
  83. }
  84.  
  85. static void TestConstruction( Article Article1 )
  86. {
  87.     VarBuf BufA = VBCreate();
  88.     VarBuf BufB = VBCreate();
  89.     VarBuf BufC = VBCreate();
  90.     VarBuf BufD = VBCreate();
  91.     VarBuf BufE = VBCreate();
  92.     VarBuf BufF = VBCreate();
  93.     VarBuf ArticleVB = NULL;
  94.  
  95.     VBPrintf( BufA, "A" );
  96.     VBPrintf( BufB, "B" );
  97.     VBPrintf( BufC, "C" );
  98.     VBPrintf( BufD, "D\n\n\n\n\n" );
  99.     VBPrintf( BufE, "E" );
  100.     VBPrintf( BufF, "F" );
  101.  
  102.     ArticleSetHeaderLine( Article1, HL_SUBJECT, BufA );
  103.     ArticleSetHeaderLine( Article1, HL_FROM, BufB );
  104.     ArticleSetHeaderLine( Article1, HL_DATE, BufE );
  105.     ArticleSetHeaderLine( Article1, HL_LINES, BufF );
  106.  
  107.     /* Test overwrite of headerline. */
  108.     ArticleSetHeaderLine( Article1, HL_NEWSGROUPS, BufA );
  109.     ArticleSetHeaderLine( Article1, HL_NEWSGROUPS, BufC );
  110.  
  111.     ArticleSetBody( Article1, BufD );
  112.  
  113.     if ( !ArticleIsOK( Article1 ) )
  114.     {
  115.     SIOPrintf( StandardError, "%V", 
  116.                        ArticleGetErrorMessages(Article1) );
  117.     exit( EXIT_FAILURE );
  118.     }
  119.  
  120.     ArticleVB = ArticleGetVB( Article1 );
  121.  
  122.     if ( strcmp( VBAsString(ArticleVB),
  123.      "Subject: A\nFrom: B\nDate: E\nLines: 1\nNewsgroups: C\n\nD\n" ) )
  124.     {
  125.     SIOPrintf( StandardError,
  126.                        "Constructed article is incorrect:\n%s",
  127.                VBAsString( ArticleVB ) );
  128.     exit( EXIT_FAILURE );
  129.     }
  130.  
  131.     VBDestroy( BufA );
  132.     VBDestroy( BufB );
  133.     VBDestroy( BufC );
  134.     VBDestroy( BufD );
  135.     VBDestroy( BufE );
  136.     VBDestroy( BufF );
  137.     VBDestroy( ArticleVB );
  138. }
  139.  
  140.  
  141. static void TestReply( Article Article1 )
  142. {
  143.     Article Reply = NULL;
  144.     VarBuf ReplyVB = NULL;
  145.     VarBuf Date = VBCreateString("the current date");
  146.     VarBuf From = VBCreateString("Myself");
  147.     VarBuf Path = VBCreateString("From here to there");
  148.     VarBuf ReplyTo = VBCreateString("Me");
  149.     VarBuf Organization = VBCreateString("No way");
  150.     VarBuf Signature = VBCreateString(":-)");
  151.     VarBuf X_NewsReader = VBCreateString("skim");
  152.  
  153.     assert( ArticleIsOK( Article1 ) );
  154.  
  155.     Reply = ArticleConstructReply( Article1, From, Path, ReplyTo,
  156.                                    Organization, Signature,
  157.                    X_NewsReader );
  158.  
  159.     ArticleSetHeaderLine( Reply, HL_DATE, Date );
  160.  
  161.     assert( ArticleIsOK( Reply ) );
  162.  
  163.     ReplyVB = ArticleGetVB( Reply );
  164.  
  165.     assert( ReplyVB != NULL );
  166.  
  167.     if ( strcmp( VBAsString(ReplyVB),
  168.          "From: Myself\n" 
  169.      "Path: From here to there\n"
  170.      "Reply-To: Me\n"
  171.      "Organization: No way\n"
  172.      "X-Newsreader: skim\n"
  173.      "Date: the current date\n"
  174.      "Lines: 5\n"
  175.      "Newsgroups: C\n"
  176.      "Subject: Re: A\n"
  177.      "\n"
  178.      "B wrote:\n"
  179.      ">D\n"
  180.      "\n"
  181.      "-- \n"
  182.      ":-)\n"
  183.        ) )
  184.     {
  185.     SIOPrintf( StandardError, "Reply is incorrect:\n%V", ReplyVB );
  186.     exit( EXIT_FAILURE );
  187.     }
  188.  
  189.     ArticleDestroy( Reply );
  190.     VBDestroy( ReplyVB );
  191.     VBDestroy( From );
  192.     VBDestroy( Path );
  193.     VBDestroy( ReplyTo );
  194.     VBDestroy( Organization );
  195.     VBDestroy( Signature );
  196.     VBDestroy( X_NewsReader );
  197.     VBDestroy( Date );
  198. }
  199.  
  200. static void TestConstructNew( void )
  201. {
  202.     Article NewArticle = NULL;
  203.     VarBuf NewsGroups = VBCreateString("alt.test");
  204.     VarBuf Subject = VBCreateString("test, please ignore");
  205.     VarBuf From = VBCreateString("Myself");
  206.     VarBuf Date = VBCreateString("the current date");
  207.     VarBuf Path = VBCreateString("From here to there");
  208.     VarBuf ReplyTo = VBCreateString("Me");
  209.     VarBuf Organization = VBCreateString("No way");
  210.     VarBuf Signature = VBCreateString(":-)");
  211.     VarBuf X_NewsReader = VBCreateString("skim");
  212.     VarBuf NewArticleVB = NULL;
  213.  
  214.     NewArticle = ArticleConstructNew(
  215.            NewsGroups, Subject, From, Path, ReplyTo,
  216.            Organization, Signature, X_NewsReader );
  217.  
  218.     ArticleSetHeaderLine( NewArticle, HL_DATE, Date );
  219.  
  220.     NewArticleVB = ArticleGetVB( NewArticle );
  221.  
  222.     if ( strcmp( VBAsString(NewArticleVB),
  223.          "From: Myself\n" 
  224.      "Path: From here to there\n"
  225.      "Reply-To: Me\n"
  226.      "Organization: No way\n"
  227.      "X-Newsreader: skim\n"
  228.      "Date: the current date\n"
  229.      "Lines: 5\n"
  230.      "Newsgroups: alt.test\n"
  231.      "Subject: test, please ignore\n"
  232.      "\n"
  233.      DEFAULT_BODY_FOR_NEW_ARTICLE
  234.      "\n-- \n"
  235.      ":-)\n"
  236.        ) )
  237.     {
  238.     SIOPrintf( StandardError, 
  239.                        "New article is incorrect:\n%V", NewArticleVB );
  240.     exit( EXIT_FAILURE );
  241.     }
  242.  
  243.     ArticleDestroy( NewArticle );
  244.     VBDestroy( From );
  245.     VBDestroy( Path );
  246.     VBDestroy( ReplyTo );
  247.     VBDestroy( Date );
  248.     VBDestroy( Organization );
  249.     VBDestroy( Signature );
  250.     VBDestroy( X_NewsReader );
  251.     VBDestroy( NewArticleVB );
  252.     VBDestroy( Subject );
  253.     VBDestroy( NewsGroups );
  254. }
  255.  
  256. int main( void )
  257. {
  258.     Article Article1;
  259.  
  260.     TestCreation( &Article1 );
  261.  
  262.     TestMinimalNewArticle();
  263.  
  264.     TestConstruction( Article1 );
  265.  
  266.     TestConstructNew();
  267.  
  268.     TestReply( Article1 );
  269.  
  270.     ArticleDestroy( Article1 );
  271.  
  272.     SIOPrintf( StandardOutput, "OK\n" );
  273.  
  274.     return EXIT_SUCCESS;
  275. }
  276.